﻿Imports HMSDataLayer
Imports HMSLibrary
Imports Microsoft.ApplicationBlocks.Data
Imports System.Data

Public Class PatientVisitWindow

    Private Sub CloseButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles CloseButton.Click
        Me.Close()
    End Sub

    Private Sub MinimizeButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles MinimizeButton.Click
        Me.WindowState = Windows.WindowState.Minimized
    End Sub

    Private Sub PatientVisitWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        'Dim dataProvider As New PatientDataLayer()
        'Dim lstCl As List(Of Lookups)
        'lstCl = dataProvider.GetPatientNameIDList()
        'PatientNameComboBox.ItemsSource = lstCl
        PatientControl.LoadData()
    End Sub

    Private Sub PatientVisitWindow_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Me.MouseDown
        Try
            DragMove()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub TransSidePanel_AddButtonClickEvent() Handles TransSidePanel.AddButtonClickEvent
        'PatientNameComboBox.SelectedIndex = -1
        AddNew()
    End Sub

    Private Sub ApplyPermission()
        If AppCommon.loginInfo.IsSuperUser = True Then
            TransSidePanel.AddVisibility = Visibility.Visible
            TransSidePanel.FindVisibility = Visibility.Visible
            TransSidePanel.SaveVisibility = Visibility.Visible
            TransSidePanel.PrintVisibility = Visibility.Visible
            Exit Sub
        End If
        Dim mp As ModulePermissions
        mp = (From ml In AppCommon.loginInfo.ModulesList Where ml.Name = HMSFormName.PatientVisit Select ml).FirstOrDefault
        If mp.Add = False Then TransSidePanel.AddVisibility = Visibility.Collapsed Else TransSidePanel.AddVisibility = Visibility.Visible
        If mp.Edit = False Then TransSidePanel.FindVisibility = Visibility.Collapsed Else TransSidePanel.FindVisibility = Visibility.Visible
        If mp.Print = False Then TransSidePanel.PrintVisibility = Visibility.Collapsed Else TransSidePanel.PrintVisibility = Visibility.Visible
    End Sub

    Private Sub TransSidePanel_BackButtonClickEvent() Handles TransSidePanel.BackButtonClickEvent
        ApplyPermission()
        TransSidePanel.BackVisibility = Visibility.Collapsed
    End Sub

    Private Sub TransSidePanel_CancelButtonClickEvent() Handles TransSidePanel.CancelButtonClickEvent
        Me.Close()
    End Sub

    Private Sub TransSidePanel_FindButtonClickEvent() Handles TransSidePanel.FindButtonClickEvent
        TransSidePanel.PrintVisibility = Visibility.Collapsed
        TransSidePanel.AddVisibility = Visibility.Collapsed
        TransSidePanel.SaveVisibility = Visibility.Collapsed
        TransSidePanel.FindVisibility = Visibility.Collapsed
        TransSidePanel.BackVisibility = Visibility.Visible
    End Sub

    Private Sub TransSidePanel_PrintButtonClickEvent() Handles TransSidePanel.PrintButtonClickEvent

    End Sub

    Private Sub TransSidePanel_SaveButtonClickEvent() Handles TransSidePanel.SaveButtonClickEvent
        If PatientControl.SelectedItem Is Nothing Then
            CommonMessages.ShowValidationMsg("Please select patient!")
            Exit Sub
        End If
        If PatientVisitUC.SaveData() = True Then
            Dim dataProvider As New PatientDataLayer()
            Dim lst As List(Of PatientVisitInfo)
            lst = dataProvider.GetPatientVisitInfo(PatientControl.SelectedItem.Id)
            PatientVisitUC.VisitsDataGrid.ItemsSource = lst
        End If
    End Sub

    Public Sub AddNew()
        AgeTextBox.Text = String.Empty
        PatientVisitUC.IDTextBlock.Text = String.Empty
        ComplainTextBox.Text = String.Empty
        LastVisitTextBox.Text = String.Empty
        'PatientControl.LoadData(0)
        PatientVisitUC.VisitsDataGrid.ItemsSource = Nothing
        PatientVisitUC.AddNew()
        PatientHistoryUC.AddNew()
        PatientTestUC.AddNew()
    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub

    'Private Sub PatientNameComboBox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles PatientNameComboBox.SelectionChanged
    '    If PatientNameComboBox.SelectedItem Is Nothing Then Exit Sub
    '    AddNew()
    '    PatientVisitUC.ID = PatientNameComboBox.SelectedValue
    '    PatientIDRun.Text = PatientVisitUC.ID
    '    PatientHistoryUC.ID = PatientVisitUC.ID
    '    PatientTestUC.ID = PatientVisitUC.ID
    '    Dim dataProvider As New PatientDataLayer()
    '    Dim lup As PatientVisitInfo
    '    lup = dataProvider.GetPatientLastVisitInfo(PatientNameComboBox.SelectedValue)
    '    If lup IsNot Nothing Then
    '        AgeTextBox.Text = lup.Age
    '        PatientVisitUC.IDTextBlock.Text = lup.VisitID + 1
    '        ComplainTextBox.Text = lup.Complain
    '        If String.IsNullOrEmpty(lup.Doctor) = False Then LastVisitTextBox.Text = "Report to " & lup.Doctor & " on " & lup.LastVisit Else LastVisitTextBox.Text = String.Empty
    '    End If
    '    Dim lst As List(Of PatientVisitInfo)
    '    lst = dataProvider.GetPatientVisitInfo(PatientNameComboBox.SelectedValue)
    '    PatientVisitUC.VisitsDataGrid.ItemsSource = lst
    'End Sub

    Private Sub PatientControl_SelectedValueChanged(sender As Object) Handles PatientControl.SelectedValueChanged
        If PatientControl.SelectedItem Is Nothing Then Exit Sub
        AddNew()
        PatientVisitUC.ID = PatientControl.SelectedItem.Id
        'PatientIDRun.Text = PatientVisitUC.ID
        PatientHistoryUC.ID = PatientVisitUC.ID
        PatientTestUC.ID = PatientVisitUC.ID
        Dim dataProvider As New PatientDataLayer()
        Dim lup As PatientVisitInfo
        lup = dataProvider.GetPatientLastVisitInfo(PatientControl.SelectedItem.Id)
        If lup IsNot Nothing Then
            AgeTextBox.Text = lup.Age
            PatientVisitUC.IDTextBlock.Text = lup.VisitID + 1
            ComplainTextBox.Text = lup.Complain
            If String.IsNullOrEmpty(lup.Doctor) = False Then LastVisitTextBox.Text = "Report to " & lup.Doctor & " on " & lup.LastVisit Else LastVisitTextBox.Text = String.Empty
        End If
        Dim lst As List(Of PatientVisitInfo)
        lst = dataProvider.GetPatientVisitInfo(PatientControl.SelectedItem.Id)
        PatientVisitUC.VisitsDataGrid.ItemsSource = lst
    End Sub
End Class
